﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RenShiKaoQin.Account
{
    public partial class FHExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    var result = from kqemp in ent.TKQEmployees where kqemp.FEmployeeID.Equals(Page.User.Identity.Name) select kqemp;
                    if (result.Any())
                    {
                        TKQEmployee kq = result.Single();
                        if (!kq.IsExaminant)
                        {
                            Response.Write("您无权查看此页面");
                            Response.End();
                        }
                    }
                    else
                    {
                        Response.Write("您无权查看此页面");
                        Response.End();
                    }
                }
            }
            tbStart.Attributes["readonly"] = "readonly";
            tbEnd.Attributes["readonly"] = "readonly";
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Bussiness.ExcelHelper.GridViewtoExcel(gvSPExport, "SPExport.xls");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbStart.Text == "" || tbEnd.Text == "")
            {
                return;
            }
            using (hrdbEntities ent = new hrdbEntities())
            {
                DateTime st = DateTime.Parse(tbStart.Text);
                DateTime end = DateTime.Parse(tbEnd.Text);
                var rrec = from trec in ent.TKQRequestRecords join theadship in ent.THeadShips on trec.FEmployeeID equals theadship.FEmployeeID into th from dd in th.DefaultIfEmpty() where (trec.StartDate >= st && trec.StartDate <= end && trec.ExamStatus == true && trec.CheckStatus == true && (trec.TypeCode.CompareTo("12") <= 0 || trec.TypeCode.Equals("60")||trec.TypeCode.Equals("61")) && dd.FValidDate == trec.TEmployee.THeadShips.Max(p => p.FValidDate)) || (trec.StartDate >= st && trec.StartDate <= end && trec.ExamStatus == true && trec.CheckStatus == true && (trec.TypeCode.CompareTo("12") <= 0 || trec.TypeCode.Equals("60")||trec.TypeCode.Equals("61")) && dd.FValidDate == null) orderby trec.TEmployee.FName, trec.TDictDept.FName select new { SerialNO = trec.SerialNO, StartDate = trec.StartDate, EndDate = trec.EndDate, EmployeeName = trec.TEmployee.FName, TypeName = trec.TDictKQType.FName, DeptName = trec.TDictDept.FName, ExamerName = trec.Examer.FName, CheckerName = trec.Checker.FName, DeptCode = trec.DeptCode, PublicID = trec.TEmployee.FPublicID, HeadshipCode = (dd.FHeadShipCode == null ? string.Empty : dd.FHeadShipCode) };
                
                if (ddlRegion.SelectedValue != "99")
                {
                    if (ddlRegion.SelectedValue == "0102")
                    {
                        rrec = rrec.Where(p => (p.DeptCode.StartsWith("01") && p.DeptCode.Length > 4) || (p.DeptCode.StartsWith("02") && p.DeptCode.Length > 4));
                    }
                    else
                    {
                        rrec = rrec.Where(p => p.DeptCode.StartsWith(ddlRegion.SelectedValue));
                    }
                }
                if (ddlPost.SelectedValue != "99")
                {
                    rrec = rrec.Where(p => p.PublicID.StartsWith(ddlPost.SelectedValue));
                }
                if (ddlHeadship.SelectedValue != "99")
                {
                    if (ddlHeadship.SelectedValue == "0605")
                    {
                        rrec = rrec.Where(p => p.HeadshipCode.StartsWith("06") && p.HeadshipCode.CompareTo("0604") <= 0);
                    }
                    else if (ddlHeadship.SelectedValue == "0608")
                    {
                        rrec = rrec.Where(p => p.HeadshipCode.StartsWith("06") && p.HeadshipCode.CompareTo("0605") > 0 && p.HeadshipCode.CompareTo("0608") <= 0);
                    }
                    else
                    {
                        rrec=rrec.Where(p=>p.HeadshipCode.StartsWith(ddlHeadship.SelectedValue));
                    }
                }
                gvSPExport.DataSource = rrec;
                gvSPExport.DataBind();
            }
        }
    }
}